#include <Wire.h>
#include <WiFi.h>
#include "TSL2561.h"
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
#include <WiFiUdp.h>
#include <Arduino.h>
#include <esp32-hal-dac.h>//DAC功能引用该库，可以不进行#include<>引用
#include "EEPROM.h"
const char* ssid     = "DTY";
const char* password = "dtydty1992";
IPAddress local_IP(192, 168, 31, 101);
IPAddress gateway(192, 168, 31, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(218, 85, 152, 99); //optional
IPAddress secondaryDNS(218, 85, 157, 99); //optional
const int udpPort = 6667;
uint16_t udpPort_send = 6666;
IPAddress node1_IP(192, 168, 31, 198);
WiFiUDP udp;

String comdata = "";
int i, j;
bool mark = false;
int dataByte[2] = { 0 };
uint8_t v_sub[2] = {0};
TSL2561 tsl2561(&Wire, TSL2561_ADDR_FLOAT);
Adafruit_TSL2591 tsl2591 = Adafruit_TSL2591(2591);
uint32_t lum2561, lum2591;
uint16_t ir2561, full2561, ir2591, full2591;
uint8_t verify[2] = {0xff, 0x01};
#define EEPROM_SIZE 2
byte eepdata[2] = {0};
void setup(void) {
  Serial.begin(115200);
  EEPROM.begin(EEPROM_SIZE);
  pinMode(DAC1, OUTPUT);
  pinMode(DAC2, OUTPUT);
  eepdata[0] = byte(EEPROM.read(0));
  eepdata[1] = byte(EEPROM.read(1));
  dacWrite(DAC1, eepdata[0]);
  dacWrite(DAC2, eepdata[1]);
  WiFi.mode(WIFI_STA);
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
  }
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("connected");
  Wire.begin(19, 18, 400000);
  // Wire1.begin(26, 25, 400000);
  tsl2561.begin();
  tsl2591.begin(&Wire, TSL2591_ADDR);
  configureSensor();
  udp.begin(udpPort);
}

void loop(void) {

  sensorRead();

  comRead();
  udpRead();
}
